//
//  UIViewController+Extend.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/7.
//

#import "UIViewController+Extend.h"
#import <objc/runtime.h>

static const char popoverAnimatorKey;

@implementation UIViewController (Extend)

-(CGFloat)RBNavigationBarH{
    return self.navigationController.navigationBar.frame.size.height + [[UIApplication sharedApplication] statusBarFrame].size.height;
}
-(CGFloat)RBTabBarH{
    return self.tabBarController.tabBar.bounds.size.height;
}
-(CGFloat)RBStatusBarH{
    return [[UIApplication sharedApplication] statusBarFrame].size.height;
}

- (RBPopoverAnimator *)popoverAnimator{
    return objc_getAssociatedObject(self, &popoverAnimatorKey);
}
-(void)setPopoverAnimator:(RBPopoverAnimator *)popoverAnimator{
    objc_setAssociatedObject(self, &popoverAnimatorKey, popoverAnimator, OBJC_ASSOCIATION_RETAIN) ;
}

- (void)rb_bottomPresentController:(UIViewController *)vc presentedHeight:(CGFloat)height completeHandle:(RBCompleteHandle)completion{
    [self rb_bottomPresentController:vc alpha:0.2 presentedHeight:height completeHandle:completion];
}
- (void)rb_bottomPresentController:(UIViewController *)vc
                             alpha:(CGFloat)alpha presentedHeight:(CGFloat)height completeHandle:(RBCompleteHandle)completion{
    self.popoverAnimator = [RBPopoverAnimator popoverAnimatorWithStyle:RBPopoverTypeActionSheet completeHandle:completion];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.transitioningDelegate = self.popoverAnimator;
    [self.popoverAnimator setBottomViewHeight:height];
    [self.popoverAnimator setCoverViewAlpha:alpha];
    [self presentViewController:vc animated:YES completion:nil];
}
- (void)rb_centerPresentController:(UIViewController *)vc presentedSize:(CGSize)size completeHandle:(RBCompleteHandle)completion{
    [self rb_centerPresentController:vc alpha:0.2 presentedSize:size completeHandle:completion];
}
- (void)rb_centerPresentController:(UIViewController *)vc alpha:(CGFloat)alpha
                     presentedSize:(CGSize)size completeHandle:(RBCompleteHandle)completion{
    self.popoverAnimator = [RBPopoverAnimator popoverAnimatorWithStyle:RBPopoverTypeAlert completeHandle:completion];
    [self.popoverAnimator setCenterViewSize:size];
    [self.popoverAnimator setCoverViewAlpha:alpha];
    vc.modalPresentationStyle = UIModalPresentationCustom;
    vc.transitioningDelegate = self.popoverAnimator;
    [self presentViewController:vc animated:YES completion:nil];
}
@end
